package com.sie.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.sie.dto.EmpFindPageDto;
import com.sie.entity.UserExcelImportTemplate;
import com.sie.entity.ViewEmp;
import com.sie.service.EmpService;
import com.sie.util.AsdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 表格导出
 */
@RestController
@CrossOrigin
@SuppressWarnings("all")
@RequestMapping("/excel")
@Api(tags = "表格导出")
public class ExcelExportController {
    @Autowired
    private EmpService service;

    @ApiOperation("表格导出")
    @PostMapping("/export")
    public void empExport(@ApiParam("entity") @RequestBody EmpFindPageDto dto, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //验证权限
        if (!AsdUtils.asd(request, "查询用户")) {
            return;
        }
//        声明文件输出流
        ServletOutputStream outputStream = null;
//        查询导出数据
        Map byPage = service.findByPage(request, dto.getEntity(), dto.getBigTime(), dto.getSmallTime(), dto.getPageNum(), dto.getSize());
        List list = (List) byPage.get("data");
//        配置表信息
        ExportParams exportParams = new ExportParams(null, "sheet1");
        //创建excel对象
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, ViewEmp.class, list);
        try {
//            配置响应头信息
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("用户列表.xlsx", "utf-8"));
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
//            获取文件输出流
            outputStream = response.getOutputStream();
//            写入响应文件
            workbook.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            关闭流
            if (outputStream != null)
                outputStream.close();
            if (workbook != null)
                workbook.close();
        }

    }

    /**
     * 表格模板导出
     * @param response
     * @throws IOException
     */
    @ApiOperation("表格模板导出")
    @PostMapping("/getUserExportTemplate")
    public void getUserExcelImportTemplate(HttpServletResponse response) throws IOException {
//        声明文件输出流
        ServletOutputStream outputStream = null;
//        配置表信息
        ExportParams exportParams = new ExportParams(null, "sheet1");
        ArrayList<UserExcelImportTemplate> list = new ArrayList<>();
        list.add(new UserExcelImportTemplate("test","男or女","18","2022-01-01","有效or无效","135********","123@qq.com","数据模板,新增前请删除!","******","******"));
        //创建excel对象
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, UserExcelImportTemplate.class,list);
        try {
//            配置响应头信息
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("用户批量导入模板.xlsx", "utf-8"));
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
//            获取文件输出流
            outputStream = response.getOutputStream();
//            写入响应文件
            workbook.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            关闭流
            if (outputStream != null)
                outputStream.close();
            if (workbook != null)
                workbook.close();
        }

    }
}
